<template>
  <el-drawer :title="title" v-model="dialogVisible" size="70%">

    <el-form :model="searchForm" size="small">
      <el-form-item label="时间选择" prop="type">
        <el-radio-group v-model="searchForm.type">
          <el-radio-button label="all">全部</el-radio-button>
          <el-radio-button label="today">今天</el-radio-button>
          <el-radio-button label="yesterday">昨天</el-radio-button>
          <el-radio-button label="last7days">最近7天</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="开始时间" prop="starttime">
        <el-date-picker v-model="searchForm.starttime" type="date" size="small" placeholder="开始时间" 
          style="width: 90%" value-format="YYYY-MM-DD" />
      </el-form-item>

      <el-form-item label="结束时间" prop="endtime">
        <el-date-picker v-model="searchForm.endtime" type="date" size="small" placeholder="结束时间" 
          style="width: 90%" value-format="YYYY-MM-DD" />
      </el-form-item>

      <el-form-item label="用户类型" prop="level">
        <el-radio-group v-model="searchForm.level">
          <el-radio-button :label="0">全部</el-radio-button>
          <el-radio-button :label="1">一级推广</el-radio-button>
          <el-radio-button :label="2">二级推广</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getData">搜索</el-button>
        <el-button @click="resetSearchForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <template v-if="type === 'user'">
        <el-table-column prop="id" label="ID" align="center" />
        <el-table-column label="会员" width="65">
          <template #default="{ row }">
            <div class="flex items-center">
              <el-avatar :size="40" :src="row.avatar" @error="() => true">
                <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
              </el-avatar>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户信息" prop="username" />
        <el-table-column label="推广数" prop="share_num" align="center"/>
        <el-table-column label="推广订单数" prop="share_order_num" align="center"/>
        <el-table-column label="绑定时间" prop="create_time" align="center"/>
      </template>

      <template v-else>
        <el-table-column label="订单号">
          <template #default="{ row }">
            {{ row.order.no }}
          </template>
        </el-table-column>

        <el-table-column label="昵称|电话|姓名">
          <template #default="{ row }">
            <div v-if="!row.order.user">该用户已被删除</div>
            <div v-else>{{ row.order.user.username }} | {{ row.order.user.nickname }} | {{ row.order.user.phone }}</div>
          </template>
        </el-table-column>

        <el-table-column label="时间" prop="create_time"></el-table-column>
        <el-table-column label="返佣金额" prop="commission"></el-table-column>
      </template>

    </el-table>

    <div class="flex items-center justify-center mt-5">
      <el-pagination background layout="prev, pager, next" :total="total" :current-page="currentPage"
        :page-size="limit" @current-change="getData"/>
    </div>
  </el-drawer>
</template>

<script setup>
  import { ref, computed } from 'vue'
  import { getAgentListApi, getAgentOrderListApi } from '@/api/distribution.js'
  import Search from '@/components/Search.vue'
  import SearchItem from '@/components/SearchItem.vue'
  import { useInitTable } from '@/hooks/useCommon.js'

  const props = defineProps({
    type: String,
    default: 'user'
  })

  const title = computed(() => props.type === 'user' ? '推广人列表' : '推广订单列表')

  const {
    searchForm,
    // resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
  } = useInitTable({
    // 搜索需要的参数
    searchForm: {
      level: 0,
      user_id: 0,
      type: 'all',
      starttime: null,
      endtime: null
    },
    // 获取表单的api
    // getlist: getAgentListApi,
    getlist: (() => {
      return props.type === 'user' ? getAgentListApi : getAgentOrderListApi
    })(),
    // 获取表单成功的回调
    onGetListSuccess: (res) => {
      tableData.value = res.list
      total.value = res.totalCount
    },
  })

  const dialogVisible = ref(false)
  function open(id) {
    searchForm.user_id = id
    dialogVisible.value = true
    getData()
  }

  // 重置会将searchForm中的数据全部重置，因此自定义重置方法
  const resetSearchForm = () => {
    searchForm.type = 'all'
    searchForm.starttime = null
    searchForm.endtime = null
    searchForm.level = 0
  }

  defineExpose({
    open
  })
</script>

<style lang="scss" scoped></style>
